package com.xframe.xdal.core.param;

import com.xframe.xdal.core.constant.DataSourceTypeEnums;
import com.xframe.xdal.core.param.builder.MySqlParamFactory;
import com.xframe.xdal.core.param.builder.OracleParamFactory;

import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
 * 参数化对象运行上下文
 *
 * @author xufeng
 * @version 1.0
 * @date 2022-09-19
 */
public class ParamContext {

    /**
     * IBuilderFactory 工厂接口
     */
    IBuilderFactory factory = null;
    /**
     * 上下文构造函数
     * @param dataSourceTypeEnums 数据源类型
     *
     */
    public ParamContext(DataSourceTypeEnums dataSourceTypeEnums){
        if(dataSourceTypeEnums == DataSourceTypeEnums.MySql || dataSourceTypeEnums == DataSourceTypeEnums.ClickHouse){
            factory = MySqlParamFactory.getInstance();
        }else {
            factory = OracleParamFactory.getInstance();
        }
    }
    /**
     * 上下文构造函数
     * @param pst SQL参数化对象
     * @param index 下标索引
     * @param param 值
     */
    public void setParam(PreparedStatement pst, Integer index, Object param) throws SQLException {
        if(param != null) {
            IParamSet paramSet = factory.getParamSet(param.getClass());
            paramSet.valueSet(pst, index, param);
        }else {
            pst.setObject(index +1,null);
        }
    }

}
